package com.cqupt.slewing.welcome

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import com.cqupt.slewing.MyViewModel
import com.cqupt.slewing.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class RegisterFragment : Fragment() {
    private lateinit var myViewModel: MyViewModel
    private lateinit var editTextUserName: EditText
    private lateinit var editTextPassword: EditText
    private lateinit var editTextPasswordConfirm: EditText
    private lateinit var buttonRegister: Button

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_register, container, false)
        editTextUserName = view.findViewById(R.id.editTextUserName)
        editTextPassword = view.findViewById(R.id.editTextPassword)
        editTextPasswordConfirm = view.findViewById(R.id.editTextPasswordConfirm)
        buttonRegister = view.findViewById(R.id.buttonRegister)
        return view
    }

    override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)
        myViewModel = ViewModelProvider(requireActivity())[MyViewModel::class.java]

        buttonRegister.setOnClickListener {
            val username = editTextUserName.text.toString()
            val password = editTextPassword.text.toString()
            val passwordConfirm = editTextPasswordConfirm.text.toString()

            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(
                    requireActivity(),
                    "Please enter username or password!",
                    Toast.LENGTH_SHORT
                ).show()
            } else if (password != passwordConfirm) {
                Toast.makeText(
                    requireActivity(),
                    "Please confirm password.",
                    Toast.LENGTH_SHORT
                ).show()
            } else {
                lifecycleScope.launch(Dispatchers.Main) {
                    if (myViewModel.userRegister(username, password)) {
                        Toast.makeText(
                            requireActivity(),
                            "Register successfully!",
                            Toast.LENGTH_SHORT
                        ).show()
                        val navController = Navigation.findNavController(it)
                        navController.navigate(R.id.action_registerFragment_to_loginFragment)
                    } else {
                        Toast.makeText(
                            requireActivity(),
                            "Username is duplicated",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }
            }
        }


    }

}